This notebook will examine the effectiveness of training a Radial Basis Function (RBF) network for a binary classification task on a two-dimensional dataset with 500 instances.

First we must load the dataset:

In [1]:
library("corpcor")
circle<-read.table("circle.txt",sep=",",header=T)
circle
V1V2labels
-0.36125433 0.77676658 1
0.35104327-0.83239241 1
-0.35627285 0.92975989 1
-0.03069027-0.18070132-1
0.94496021 0.97921835 1
-0.70625080-0.99694213 1
0.99161877 0.99690349 1
0.83584269 0.30713464 1
-0.17736194 0.90564358 1
-0.17756695 0.37130143-1
0.76197443-0.20293592-1
0.40542301 0.37005498-1
0.04536701 0.92422922 1
0.48410537-0.61154208-1
-0.30480615-0.61528897-1
-0.87435384-0.47106882 1
0.21920208 0.31123623-1
0.10303092 0.31947664-1
-0.35033518-0.59395713-1
-0.52018974 0.79206822 1
0.12044941 0.64084236-1
-0.25619249 0.46443524-1
0.34310328 0.49858198-1
0.16621527 0.32838838-1
-0.03588056-0.76888308-1
0.05041501-0.17397570-1
-0.95241036-0.62292148 1
0.86504276-0.45130243 1
0.54569805 0.06156481-1
-0.85166635-0.01448331 1
.........
-0.540349246-0.47263244 -1
-0.288294246 0.01323212 -1
-0.193947457 0.42115608 -1
0.274749820 0.12387255 -1
0.218212669 0.35833028 -1
0.021630626-0.31190543 -1
0.469162588-0.04312447 -1
-0.917217458 0.67389973 1
-0.797764752 0.70758251 1
-0.853099801 0.02438959 1
-0.558226953 0.74080376 1
0.008213195-0.80724706 1
-0.875090029-0.33855274 1
-0.943369016-0.19136623 1
0.311614896 0.26388053 -1
0.675920213 0.39697837 -1
0.044039452-0.15464237 -1
-0.198123230-0.97226879 1
-0.136938435 0.07462970 -1
-0.889119328-0.35960385 1
-0.278381032-0.62779693 -1
-0.974760894-0.63087402 1
0.449694932 0.26733192 -1
-0.617905315 0.41266753 -1
-0.282400398 0.08456959 -1
0.639366953-0.41557695 -1
-0.972901764-0.21816634 1
0.690068852 0.31366387 -1
-0.849925071-0.57528912 1
-0.108876550-0.98229180 1

Next we visualize the dataset:

In [2]:
class.index<-dim(circle)[2]
plot(circle[,]$V1,circle[,]$V2,xlim=c(-1,1),ylim=c(-1,1),col=c("blue","black","red")[circle[,]$labels+2])

Next we perform a 70-30 train/test split:

In [3]:
train.index<-sample(nrow(circle),nrow(circle)*0.3)
train.index
  1. 459
  2. 354
  3. 13
  4. 482
  5. 116
  6. 136
  7. 316
  8. 213
  9. 224
  10. 451
  11. 467
  12. 360
  13. 267
  14. 193
  15. 284
  16. 137
  17. 328
  18. 53
  19. 182
  20. 321
  21. 293
  22. 118
  23. 106
  24. 286
  25. 162
  26. 473
  27. 203
  28. 130
  29. 313
  30. 307
  31. 143
  32. 234
  33. 361
  34. 222
  35. 20
  36. 109
  37. 289
  38. 488
  39. 493
  40. 161
  41. 351
  42. 231
  43. 465
  44. 400
  45. 469
  46. 122
  47. 228
  48. 425
  49. 31
  50. 475
  51. 241
  52. 315
  53. 325
  54. 412
  55. 89
  56. 139
  57. 445
  58. 190
  59. 107
  60. 128
  61. 129
  62. 363
  63. 40
  64. 288
  65. 382
  66. 146
  67. 218
  68. 246
  69. 249
  70. 368
  71. 110
  72. 344
  73. 205
  74. 366
  75. 420
  76. 287
  77. 86
  78. 399
  79. 320
  80. 463
  81. 113
  82. 336
  83. 210
  84. 499
  85. 219
  86. 103
  87. 72
  88. 370
  89. 387
  90. 214
  91. 461
  92. 14
  93. 491
  94. 441
  95. 173
  96. 9
  97. 314
  98. 101
  99. 403
  100. 145
  101. 436
  102. 230
  103. 279
  104. 135
  105. 29
  106. 453
  107. 407
  108. 108
  109. 92
  110. 317
  111. 304
  112. 275
  113. 494
  114. 281
  115. 27
  116. 142
  117. 409
  118. 414
  119. 381
  120. 244
  121. 259
  122. 364
  123. 12
  124. 232
  125. 17
  126. 339
  127. 73
  128. 376
  129. 202
  130. 401
  131. 97
  132. 483
  133. 127
  134. 415
  135. 81
  136. 377
  137. 212
  138. 338
  139. 245
  140. 326
  141. 112
  142. 337
  143. 318
  144. 120
  145. 324
  146. 367
  147. 402
  148. 33
  149. 330
  150. 183
In [4]:
training.set<-circle[train.index,]
training.set
V1V2labels
459-0.953707702 0.158214991 1
354 0.377162058 0.739209653 1
13 0.045367011 0.924229216 1
482 0.008213195-0.807247063 1
116 0.878132381-0.180204436 1
136 0.227569337 0.734268232-1
316-0.300581376-0.898516963 1
213 0.512752345-0.847260452 1
224 0.159647269-0.642042277-1
451-0.334873488-0.874592004 1
467 0.966850032 0.165503697 1
360-0.375843487-0.443063847-1
267 0.980529896 0.050735979 1
193 0.855933436 0.124292508 1
284-0.489151232-0.194107711-1
137 0.596438672-0.007048026-1
328 0.770281476-0.830577015 1
53-0.241049260 0.951546647 1
182 0.305645943 0.736848090-1
321-0.383414764-0.150402702-1
293-0.774137055-0.357319204 1
118 0.436506887 0.395508170-1
106 0.508584236 0.146206370-1
286-0.723368590-0.938728564 1
162-0.508722295-0.045524521-1
473-0.193947457 0.421156085-1
203 0.941996058 0.167771324 1
130-0.054662157-0.350945920-1
313 0.766390139-0.738573610 1
307-0.758978200 0.251453501 1
............
259-0.29441110-0.33014643-1
364-0.75177466-0.07522496-1
12 0.40542301 0.37005498-1
232 0.77883081-0.95724206 1
17 0.21920208 0.31123623-1
339-0.38465707 0.92792477 1
73 0.83676766-0.14598232 1
376 0.78070002 0.77897919 1
202 0.01568903 0.82037835 1
401 0.86044316-0.89068916 1
97 0.44605799 0.19852044-1
483-0.87509003-0.33855274 1
127-0.40170875-0.74819244 1
415-0.42678032-0.08516329-1
81 0.06502816 0.65579991-1
377 0.22339528-0.75587107-1
212-0.54602492-0.64458571 1
338-0.39482403 0.78707272 1
245 0.93664657-0.32375123 1
326-0.78678679 0.99568614 1
112-0.54160832-0.13024936-1
337-0.97215897-0.97676421 1
318-0.33170215 0.05055498-1
120 0.44858381 0.38951698-1
324-0.09113681 0.11145868-1
367-0.92335899-0.35558594 1
402-0.40984593 0.92798257 1
33 0.36475743-0.26425190-1
330 0.34731486-0.02724104-1
183-0.29636395-0.92788027 1
In [5]:
training.set.features<-training.set[,-class.index]
training.set.labels<-training.set[,class.index]
training.set.labels
  1. 1
  2. 1
  3. 1
  4. 1
  5. 1
  6. -1
  7. 1
  8. 1
  9. -1
  10. 1
  11. 1
  12. -1
  13. 1
  14. 1
  15. -1
  16. -1
  17. 1
  18. 1
  19. -1
  20. -1
  21. 1
  22. -1
  23. -1
  24. 1
  25. -1
  26. -1
  27. 1
  28. -1
  29. 1
  30. 1
  31. 1
  32. -1
  33. -1
  34. -1
  35. 1
  36. -1
  37. 1
  38. 1
  39. -1
  40. -1
  41. -1
  42. 1
  43. -1
  44. -1
  45. -1
  46. 1
  47. 1
  48. 1
  49. 1
  50. -1
  51. 1
  52. -1
  53. -1
  54. 1
  55. -1
  56. -1
  57. 1
  58. 1
  59. 1
  60. 1
  61. 1
  62. 1
  63. 1
  64. 1
  65. -1
  66. 1
  67. -1
  68. -1
  69. -1
  70. 1
  71. -1
  72. -1
  73. 1
  74. -1
  75. -1
  76. -1
  77. -1
  78. 1
  79. 1
  80. 1
  81. -1
  82. -1
  83. 1
  84. 1
  85. 1
  86. -1
  87. -1
  88. 1
  89. 1
  90. -1
  91. 1
  92. -1
  93. -1
  94. -1
  95. 1
  96. 1
  97. -1
  98. -1
  99. -1
  100. -1
  101. -1
  102. 1
  103. -1
  104. -1
  105. -1
  106. -1
  107. -1
  108. 1
  109. 1
  110. 1
  111. -1
  112. 1
  113. -1
  114. -1
  115. 1
  116. 1
  117. 1
  118. -1
  119. -1
  120. 1
  121. -1
  122. -1
  123. -1
  124. 1
  125. -1
  126. 1
  127. 1
  128. 1
  129. 1
  130. 1
  131. -1
  132. 1
  133. 1
  134. -1
  135. -1
  136. -1
  137. 1
  138. 1
  139. 1
  140. 1
  141. -1
  142. 1
  143. -1
  144. -1
  145. -1
  146. 1
  147. 1
  148. -1
  149. -1
  150. 1
In [6]:
test.set<-circle[-train.index,]
test.set
V1V2labels
1-0.36125433 0.77676658 1
2 0.35104327-0.83239241 1
3-0.35627285 0.92975989 1
4-0.03069027-0.18070132-1
5 0.94496021 0.97921835 1
6-0.70625080-0.99694213 1
7 0.99161877 0.99690349 1
8 0.83584269 0.30713464 1
10-0.17756695 0.37130143-1
11 0.76197443-0.20293592-1
15-0.30480615-0.61528897-1
16-0.87435384-0.47106882 1
18 0.10303092 0.31947664-1
19-0.35033518-0.59395713-1
21 0.12044941 0.64084236-1
22-0.25619249 0.46443524-1
23 0.34310328 0.49858198-1
24 0.16621527 0.32838838-1
25-0.03588056-0.76888308-1
26 0.05041501-0.17397570-1
28 0.86504276-0.45130243 1
30-0.85166635-0.01448331 1
32 0.02897208-0.71931711-1
34-0.88241886-0.13160632 1
35 0.52264887-0.33377461-1
36 0.19606415 0.88273518 1
37-0.76806530 0.71662675 1
38 0.24970607-0.77653605 1
39 0.12309184-0.17683170-1
41 0.42489162 0.60078024-1
............
456-0.67188690-0.21608583-1
457 0.73972739-0.01691149-1
458 0.49326846 0.64142884 1
460-0.22028515-0.04412977-1
462 0.64844526-0.02676766-1
464 0.04487604-0.39088353-1
466 0.83326895 0.40689094 1
468 0.72768002-0.66586805 1
470-0.52082891 0.39707271-1
471-0.54034925-0.47263244-1
472-0.28829425 0.01323212-1
474 0.27474982 0.12387255-1
476 0.02163063-0.31190543-1
477 0.46916259-0.04312447-1
478-0.91721746 0.67389973 1
479-0.79776475 0.70758251 1
480-0.85309980 0.02438959 1
481-0.55822695 0.74080376 1
484-0.94336902-0.19136623 1
485 0.31161490 0.26388053-1
486 0.67592021 0.39697837-1
487 0.04403945-0.15464237-1
489-0.13693843 0.07462970-1
490-0.88911933-0.35960385 1
492-0.97476089-0.63087402 1
495-0.28240040 0.08456959-1
496 0.63936695-0.41557695-1
497-0.97290176-0.21816634 1
498 0.69006885 0.31366387-1
500-0.10887655-0.98229180 1
In [7]:
test.set.features<-test.set[,-class.index]
test.set.labels<-test.set[,class.index]
test.set.labels
  1. 1
  2. 1
  3. 1
  4. -1
  5. 1
  6. 1
  7. 1
  8. 1
  9. -1
  10. -1
  11. -1
  12. 1
  13. -1
  14. -1
  15. -1
  16. -1
  17. -1
  18. -1
  19. -1
  20. -1
  21. 1
  22. 1
  23. -1
  24. 1
  25. -1
  26. 1
  27. 1
  28. 1
  29. -1
  30. -1
  31. 1
  32. -1
  33. 1
  34. 1
  35. -1
  36. 1
  37. 1
  38. -1
  39. 1
  40. 1
  41. -1
  42. -1
  43. -1
  44. -1
  45. 1
  46. 1
  47. 1
  48. -1
  49. 1
  50. -1
  51. 1
  52. 1
  53. 1
  54. 1
  55. -1
  56. -1
  57. -1
  58. -1
  59. -1
  60. -1
  61. -1
  62. 1
  63. -1
  64. -1
  65. 1
  66. -1
  67. -1
  68. -1
  69. 1
  70. 1
  71. -1
  72. -1
  73. 1
  74. -1
  75. -1
  76. -1
  77. 1
  78. -1
  79. 1
  80. -1
  81. -1
  82. 1
  83. 1
  84. -1
  85. -1
  86. 1
  87. -1
  88. -1
  89. 1
  90. -1
  91. -1
  92. -1
  93. 1
  94. 1
  95. 1
  96. -1
  97. -1
  98. 1
  99. -1
  100. 1
  101. 1
  102. -1
  103. 1
  104. 1
  105. -1
  106. 1
  107. 1
  108. 1
  109. 1
  110. 1
  111. -1
  112. -1
  113. 1
  114. 1
  115. -1
  116. 1
  117. 1
  118. -1
  119. -1
  120. 1
  121. 1
  122. -1
  123. 1
  124. 1
  125. 1
  126. -1
  127. -1
  128. 1
  129. -1
  130. -1
  131. -1
  132. -1
  133. 1
  134. 1
  135. -1
  136. 1
  137. -1
  138. -1
  139. 1
  140. 1
  141. 1
  142. -1
  143. 1
  144. -1
  145. -1
  146. 1
  147. 1
  148. -1
  149. 1
  150. 1
  151. -1
  152. 1
  153. 1
  154. 1
  155. -1
  156. 1
  157. 1
  158. 1
  159. -1
  160. 1
  161. 1
  162. -1
  163. 1
  164. 1
  165. 1
  166. 1
  167. -1
  168. -1
  169. -1
  170. -1
  171. -1
  172. -1
  173. -1
  174. 1
  175. -1
  176. 1
  177. -1
  178. 1
  179. 1
  180. 1
  181. -1
  182. -1
  183. -1
  184. 1
  185. 1
  186. -1
  187. 1
  188. 1
  189. -1
  190. 1
  191. 1
  192. 1
  193. 1
  194. 1
  195. -1
  196. -1
  197. 1
  198. -1
  199. -1
  200. -1
  201. 1
  202. 1
  203. 1
  204. -1
  205. -1
  206. -1
  207. -1
  208. -1
  209. 1
  210. 1
  211. -1
  212. 1
  213. 1
  214. -1
  215. 1
  216. 1
  217. -1
  218. -1
  219. -1
  220. 1
  221. -1
  222. -1
  223. -1
  224. 1
  225. 1
  226. -1
  227. -1
  228. 1
  229. -1
  230. 1
  231. -1
  232. -1
  233. -1
  234. 1
  235. 1
  236. 1
  237. -1
  238. -1
  239. 1
  240. 1
  241. 1
  242. 1
  243. -1
  244. -1
  245. 1
  246. 1
  247. 1
  248. 1
  249. -1
  250. 1
  251. -1
  252. -1
  253. -1
  254. -1
  255. -1
  256. -1
  257. -1
  258. 1
  259. -1
  260. -1
  261. -1
  262. -1
  263. -1
  264. 1
  265. 1
  266. 1
  267. 1
  268. -1
  269. -1
  270. 1
  271. 1
  272. 1
  273. -1
  274. -1
  275. 1
  276. 1
  277. 1
  278. 1
  279. 1
  280. 1
  281. -1
  282. -1
  283. 1
  284. 1
  285. -1
  286. 1
  287. 1
  288. -1
  289. 1
  290. -1
  291. -1
  292. -1
  293. 1
  294. 1
  295. 1
  296. -1
  297. -1
  298. -1
  299. 1
  300. 1
  301. -1
  302. 1
  303. 1
  304. -1
  305. 1
  306. -1
  307. -1
  308. -1
  309. 1
  310. 1
  311. 1
  312. -1
  313. 1
  314. -1
  315. 1
  316. 1
  317. 1
  318. -1
  319. -1
  320. -1
  321. -1
  322. -1
  323. 1
  324. -1
  325. -1
  326. -1
  327. 1
  328. 1
  329. -1
  330. -1
  331. -1
  332. -1
  333. -1
  334. -1
  335. 1
  336. 1
  337. 1
  338. 1
  339. 1
  340. -1
  341. -1
  342. -1
  343. -1
  344. 1
  345. 1
  346. -1
  347. -1
  348. 1
  349. -1
  350. 1

Next we provide a function to train the RBF network and execute the training. We will train an RBF model with 2 centers, 5 centers, and 10 centers, respectively.

In [8]:
rbf <- function(X, Y, K, gamma=1.0) {
  N<- dim(X)[1] # number of instances
  
  repeat {
    km <- kmeans(X, K)  # let's cluster K centers out of the dataset
    if (min(km$size)>0) # only accept if there are no empty clusters
      break
  }
  mus <- km$centers # the clusters points
  
  Phi <- matrix(rep(NA,(K+1)*N), ncol=K+1)
  for (lin in 1:N) {
    Phi[lin,1] <- 1    # bias column
    for (col in 1:K) {
      Phi[lin,col+1] <- exp( -gamma * norm(as.matrix(X[lin,]-mus[col,]),"F")^2 )
    }
  }
#  w <- pseudoinverse(t(Phi) %*% Phi) %*% t(Phi) %*% matrix(as.numeric(Y))  # find RBF weights
  w <- pseudoinverse(Phi) %*% matrix(as.numeric(Y))  # find RBF weights
  list(weights=w, centers=mus, gamma=gamma)  # return the rbf model
}
# now call rbf function
rbf.model.2<-rbf(training.set.features,training.set.labels, 2)
rbf.model.2
$weights
2.623843
-2.386206
-3.022563
$centers
V1V2
-0.11450799-0.5841307
0.08854528 0.4610525
$gamma
1
In [9]:
rbf.model.5<-rbf(training.set.features,training.set.labels, 5)
rbf.model.5
$weights
3.999352
-2.826701
-2.080293
-1.608306
-1.024512
-2.039939
$centers
V1V2
0.46514543 0.3614045
-0.61532480-0.3645666
0.67132315-0.6182762
-0.07719623-0.7495990
-0.52348814 0.6689460
$gamma
1
In [10]:
rbf.model.10<-rbf(training.set.features,training.set.labels, 10)
rbf.model.10
$weights
5.799698
-5.908817
-4.114527
-5.153639
-2.546680
2.967465
2.361171
-7.380517
5.551373
-6.357244
4.031228
$centers
V1V2
-0.21646251-0.22203213
-0.90507141 0.90887914
0.66807916-0.79844654
-0.69304176 0.37817038
-0.09949302-0.82908969
0.64903817-0.01139611
0.40680471 0.58113402
-0.69722710-0.21572379
-0.78075151-0.68045686
-0.18400896 0.76770293
$gamma
1

Next we provide a function to make predictions using our trained models:

In [11]:
rbf.predict <- function(model, X, classification=FALSE) {  
  gamma   <- model$gamma
  centers <- model$centers
  w       <- model$weights
  N       <- dim(X)[1]    # number of observations
  
  pred <- rep(w[1],N)  # we need to init to a value, so let's start with the bias
  for (j in 1:N) {  
    # find prediction for point xj
    for (k in 1:length(centers[,1])) {
      # the weight for center[k] is given by w[k+1] (because w[1] is the bias)
      pred[j] <- pred[j] + w[k+1] * exp( -gamma * norm(as.matrix(X[j,]-centers[k,]),"F")^2 )
    }
  }
  
  if (classification) {
    pred <- unlist(lapply(pred, sign))
  }
  return(pred)
}
predictions.2<-rbf.predict(rbf.model.2,test.set.features,TRUE)
predictions.2
  1. 1
  2. 1
  3. 1
  4. -1
  5. 1
  6. 1
  7. 1
  8. 1
  9. -1
  10. 1
  11. -1
  12. 1
  13. -1
  14. -1
  15. -1
  16. -1
  17. -1
  18. -1
  19. -1
  20. -1
  21. 1
  22. 1
  23. -1
  24. 1
  25. -1
  26. -1
  27. 1
  28. -1
  29. -1
  30. -1
  31. -1
  32. -1
  33. 1
  34. 1
  35. -1
  36. 1
  37. 1
  38. -1
  39. 1
  40. 1
  41. -1
  42. -1
  43. -1
  44. -1
  45. 1
  46. 1
  47. 1
  48. -1
  49. 1
  50. -1
  51. 1
  52. 1
  53. 1
  54. 1
  55. -1
  56. -1
  57. -1
  58. -1
  59. -1
  60. -1
  61. 1
  62. 1
  63. -1
  64. -1
  65. 1
  66. -1
  67. -1
  68. -1
  69. 1
  70. 1
  71. -1
  72. 1
  73. -1
  74. -1
  75. -1
  76. -1
  77. 1
  78. -1
  79. 1
  80. -1
  81. -1
  82. 1
  83. 1
  84. 1
  85. -1
  86. 1
  87. -1
  88. -1
  89. 1
  90. -1
  91. -1
  92. -1
  93. 1
  94. 1
  95. 1
  96. -1
  97. -1
  98. 1
  99. -1
  100. 1
  101. 1
  102. -1
  103. 1
  104. -1
  105. -1
  106. 1
  107. 1
  108. 1
  109. 1
  110. 1
  111. -1
  112. -1
  113. 1
  114. 1
  115. -1
  116. 1
  117. 1
  118. -1
  119. -1
  120. 1
  121. 1
  122. -1
  123. -1
  124. 1
  125. 1
  126. -1
  127. 1
  128. 1
  129. 1
  130. -1
  131. -1
  132. 1
  133. 1
  134. 1
  135. -1
  136. 1
  137. -1
  138. -1
  139. 1
  140. 1
  141. 1
  142. -1
  143. 1
  144. -1
  145. -1
  146. 1
  147. 1
  148. -1
  149. 1
  150. 1
  151. -1
  152. 1
  153. 1
  154. 1
  155. 1
  156. -1
  157. 1
  158. 1
  159. 1
  160. 1
  161. 1
  162. -1
  163. 1
  164. 1
  165. 1
  166. 1
  167. -1
  168. -1
  169. -1
  170. 1
  171. -1
  172. -1
  173. -1
  174. 1
  175. -1
  176. 1
  177. -1
  178. 1
  179. 1
  180. 1
  181. -1
  182. -1
  183. -1
  184. 1
  185. 1
  186. -1
  187. 1
  188. 1
  189. 1
  190. -1
  191. 1
  192. 1
  193. 1
  194. 1
  195. -1
  196. -1
  197. 1
  198. -1
  199. 1
  200. -1
  201. 1
  202. 1
  203. 1
  204. -1
  205. -1
  206. -1
  207. -1
  208. -1
  209. -1
  210. 1
  211. -1
  212. 1
  213. 1
  214. -1
  215. 1
  216. 1
  217. -1
  218. -1
  219. -1
  220. 1
  221. -1
  222. -1
  223. -1
  224. 1
  225. 1
  226. 1
  227. 1
  228. -1
  229. -1
  230. -1
  231. -1
  232. -1
  233. -1
  234. 1
  235. -1
  236. -1
  237. -1
  238. 1
  239. 1
  240. 1
  241. 1
  242. 1
  243. -1
  244. -1
  245. 1
  246. -1
  247. 1
  248. 1
  249. -1
  250. 1
  251. -1
  252. -1
  253. -1
  254. -1
  255. -1
  256. -1
  257. -1
  258. 1
  259. -1
  260. -1
  261. -1
  262. -1
  263. 1
  264. 1
  265. 1
  266. 1
  267. 1
  268. -1
  269. -1
  270. 1
  271. 1
  272. 1
  273. -1
  274. -1
  275. 1
  276. 1
  277. 1
  278. 1
  279. 1
  280. 1
  281. -1
  282. -1
  283. -1
  284. 1
  285. -1
  286. 1
  287. 1
  288. -1
  289. 1
  290. -1
  291. -1
  292. -1
  293. 1
  294. 1
  295. 1
  296. -1
  297. -1
  298. -1
  299. 1
  300. 1
  301. -1
  302. 1
  303. -1
  304. -1
  305. 1
  306. -1
  307. -1
  308. -1
  309. -1
  310. 1
  311. 1
  312. -1
  313. 1
  314. 1
  315. 1
  316. 1
  317. 1
  318. -1
  319. -1
  320. -1
  321. 1
  322. 1
  323. -1
  324. -1
  325. -1
  326. -1
  327. 1
  328. 1
  329. -1
  330. -1
  331. -1
  332. -1
  333. -1
  334. -1
  335. 1
  336. 1
  337. 1
  338. 1
  339. 1
  340. -1
  341. 1
  342. -1
  343. -1
  344. 1
  345. 1
  346. -1
  347. 1
  348. 1
  349. 1
  350. 1
In [12]:
predictions.5<-rbf.predict(rbf.model.5,test.set.features,TRUE)
predictions.5
  1. 1
  2. 1
  3. 1
  4. -1
  5. 1
  6. 1
  7. 1
  8. 1
  9. -1
  10. -1
  11. -1
  12. 1
  13. -1
  14. -1
  15. -1
  16. -1
  17. -1
  18. -1
  19. -1
  20. -1
  21. 1
  22. 1
  23. -1
  24. 1
  25. -1
  26. 1
  27. 1
  28. 1
  29. -1
  30. -1
  31. 1
  32. -1
  33. 1
  34. 1
  35. -1
  36. 1
  37. 1
  38. -1
  39. 1
  40. 1
  41. -1
  42. -1
  43. -1
  44. -1
  45. 1
  46. 1
  47. 1
  48. 1
  49. 1
  50. -1
  51. 1
  52. 1
  53. 1
  54. 1
  55. -1
  56. -1
  57. -1
  58. -1
  59. -1
  60. -1
  61. -1
  62. 1
  63. -1
  64. -1
  65. 1
  66. -1
  67. -1
  68. -1
  69. 1
  70. 1
  71. -1
  72. -1
  73. 1
  74. -1
  75. -1
  76. -1
  77. 1
  78. -1
  79. 1
  80. -1
  81. -1
  82. 1
  83. 1
  84. -1
  85. -1
  86. 1
  87. -1
  88. -1
  89. 1
  90. -1
  91. -1
  92. -1
  93. 1
  94. 1
  95. 1
  96. -1
  97. -1
  98. 1
  99. -1
  100. 1
  101. 1
  102. -1
  103. 1
  104. 1
  105. -1
  106. 1
  107. 1
  108. 1
  109. 1
  110. 1
  111. -1
  112. -1
  113. 1
  114. 1
  115. -1
  116. 1
  117. 1
  118. -1
  119. -1
  120. 1
  121. 1
  122. -1
  123. 1
  124. 1
  125. 1
  126. -1
  127. -1
  128. 1
  129. -1
  130. -1
  131. -1
  132. 1
  133. 1
  134. 1
  135. -1
  136. 1
  137. -1
  138. -1
  139. 1
  140. 1
  141. 1
  142. -1
  143. 1
  144. -1
  145. 1
  146. 1
  147. 1
  148. -1
  149. 1
  150. 1
  151. -1
  152. 1
  153. 1
  154. 1
  155. -1
  156. 1
  157. 1
  158. 1
  159. -1
  160. 1
  161. 1
  162. -1
  163. 1
  164. 1
  165. 1
  166. 1
  167. -1
  168. -1
  169. -1
  170. -1
  171. -1
  172. -1
  173. -1
  174. 1
  175. -1
  176. 1
  177. -1
  178. 1
  179. 1
  180. 1
  181. -1
  182. -1
  183. -1
  184. 1
  185. 1
  186. -1
  187. 1
  188. 1
  189. -1
  190. 1
  191. 1
  192. 1
  193. 1
  194. 1
  195. -1
  196. -1
  197. 1
  198. -1
  199. -1
  200. -1
  201. 1
  202. 1
  203. 1
  204. -1
  205. -1
  206. -1
  207. -1
  208. -1
  209. 1
  210. 1
  211. -1
  212. 1
  213. 1
  214. -1
  215. 1
  216. 1
  217. -1
  218. -1
  219. -1
  220. 1
  221. -1
  222. -1
  223. -1
  224. 1
  225. 1
  226. -1
  227. 1
  228. 1
  229. -1
  230. 1
  231. 1
  232. -1
  233. -1
  234. 1
  235. 1
  236. 1
  237. -1
  238. -1
  239. 1
  240. 1
  241. 1
  242. 1
  243. -1
  244. -1
  245. 1
  246. 1
  247. 1
  248. 1
  249. -1
  250. 1
  251. -1
  252. -1
  253. -1
  254. -1
  255. -1
  256. -1
  257. -1
  258. 1
  259. -1
  260. -1
  261. -1
  262. -1
  263. -1
  264. 1
  265. 1
  266. 1
  267. 1
  268. -1
  269. -1
  270. 1
  271. 1
  272. -1
  273. -1
  274. -1
  275. 1
  276. 1
  277. 1
  278. 1
  279. 1
  280. 1
  281. -1
  282. -1
  283. 1
  284. 1
  285. -1
  286. 1
  287. 1
  288. -1
  289. 1
  290. -1
  291. -1
  292. -1
  293. 1
  294. 1
  295. 1
  296. -1
  297. -1
  298. -1
  299. 1
  300. 1
  301. -1
  302. 1
  303. 1
  304. -1
  305. 1
  306. -1
  307. -1
  308. -1
  309. 1
  310. 1
  311. 1
  312. -1
  313. 1
  314. -1
  315. 1
  316. 1
  317. 1
  318. -1
  319. -1
  320. -1
  321. -1
  322. -1
  323. 1
  324. -1
  325. -1
  326. -1
  327. 1
  328. 1
  329. -1
  330. -1
  331. -1
  332. -1
  333. -1
  334. -1
  335. 1
  336. 1
  337. 1
  338. 1
  339. 1
  340. -1
  341. -1
  342. -1
  343. -1
  344. 1
  345. 1
  346. -1
  347. -1
  348. 1
  349. -1
  350. 1
In [13]:
predictions.10<-rbf.predict(rbf.model.10,test.set.features,TRUE)
predictions.10
  1. 1
  2. 1
  3. 1
  4. -1
  5. 1
  6. 1
  7. 1
  8. 1
  9. -1
  10. 1
  11. -1
  12. 1
  13. -1
  14. -1
  15. -1
  16. -1
  17. -1
  18. -1
  19. 1
  20. -1
  21. 1
  22. 1
  23. -1
  24. 1
  25. -1
  26. 1
  27. 1
  28. 1
  29. -1
  30. -1
  31. 1
  32. -1
  33. 1
  34. 1
  35. -1
  36. 1
  37. 1
  38. -1
  39. 1
  40. 1
  41. -1
  42. -1
  43. -1
  44. -1
  45. 1
  46. 1
  47. 1
  48. 1
  49. 1
  50. -1
  51. 1
  52. 1
  53. 1
  54. 1
  55. -1
  56. -1
  57. -1
  58. -1
  59. -1
  60. -1
  61. -1
  62. 1
  63. -1
  64. -1
  65. 1
  66. -1
  67. -1
  68. -1
  69. 1
  70. 1
  71. -1
  72. 1
  73. 1
  74. -1
  75. -1
  76. -1
  77. 1
  78. -1
  79. -1
  80. -1
  81. -1
  82. 1
  83. 1
  84. -1
  85. -1
  86. 1
  87. -1
  88. -1
  89. 1
  90. -1
  91. -1
  92. -1
  93. 1
  94. 1
  95. 1
  96. -1
  97. -1
  98. 1
  99. -1
  100. 1
  101. 1
  102. -1
  103. 1
  104. 1
  105. -1
  106. 1
  107. 1
  108. 1
  109. 1
  110. 1
  111. -1
  112. -1
  113. -1
  114. 1
  115. -1
  116. 1
  117. 1
  118. -1
  119. -1
  120. 1
  121. 1
  122. -1
  123. -1
  124. 1
  125. 1
  126. -1
  127. -1
  128. 1
  129. 1
  130. -1
  131. -1
  132. 1
  133. 1
  134. 1
  135. -1
  136. 1
  137. -1
  138. -1
  139. 1
  140. 1
  141. 1
  142. -1
  143. 1
  144. -1
  145. -1
  146. 1
  147. 1
  148. -1
  149. 1
  150. 1
  151. -1
  152. 1
  153. 1
  154. 1
  155. 1
  156. 1
  157. 1
  158. 1
  159. 1
  160. 1
  161. 1
  162. -1
  163. 1
  164. 1
  165. 1
  166. 1
  167. -1
  168. -1
  169. -1
  170. 1
  171. -1
  172. -1
  173. -1
  174. 1
  175. -1
  176. 1
  177. -1
  178. 1
  179. 1
  180. 1
  181. -1
  182. -1
  183. -1
  184. 1
  185. 1
  186. -1
  187. 1
  188. 1
  189. 1
  190. 1
  191. 1
  192. 1
  193. 1
  194. 1
  195. -1
  196. -1
  197. 1
  198. -1
  199. -1
  200. -1
  201. 1
  202. 1
  203. 1
  204. -1
  205. -1
  206. -1
  207. -1
  208. -1
  209. 1
  210. 1
  211. -1
  212. 1
  213. 1
  214. 1
  215. 1
  216. 1
  217. -1
  218. -1
  219. -1
  220. 1
  221. -1
  222. -1
  223. -1
  224. 1
  225. 1
  226. -1
  227. 1
  228. 1
  229. -1
  230. 1
  231. 1
  232. -1
  233. -1
  234. 1
  235. 1
  236. 1
  237. -1
  238. -1
  239. 1
  240. 1
  241. 1
  242. 1
  243. -1
  244. -1
  245. 1
  246. 1
  247. 1
  248. 1
  249. -1
  250. 1
  251. -1
  252. -1
  253. -1
  254. -1
  255. -1
  256. -1
  257. -1
  258. 1
  259. -1
  260. -1
  261. -1
  262. -1
  263. -1
  264. 1
  265. 1
  266. 1
  267. 1
  268. -1
  269. -1
  270. 1
  271. 1
  272. 1
  273. -1
  274. -1
  275. 1
  276. 1
  277. 1
  278. 1
  279. 1
  280. 1
  281. -1
  282. -1
  283. 1
  284. 1
  285. -1
  286. 1
  287. 1
  288. -1
  289. 1
  290. -1
  291. -1
  292. -1
  293. 1
  294. 1
  295. -1
  296. -1
  297. -1
  298. -1
  299. 1
  300. 1
  301. -1
  302. 1
  303. 1
  304. -1
  305. 1
  306. -1
  307. -1
  308. -1
  309. 1
  310. 1
  311. 1
  312. -1
  313. 1
  314. -1
  315. 1
  316. 1
  317. 1
  318. -1
  319. -1
  320. -1
  321. -1
  322. 1
  323. -1
  324. -1
  325. -1
  326. -1
  327. 1
  328. 1
  329. -1
  330. -1
  331. -1
  332. -1
  333. -1
  334. -1
  335. 1
  336. 1
  337. 1
  338. 1
  339. 1
  340. -1
  341. -1
  342. -1
  343. -1
  344. 1
  345. 1
  346. -1
  347. -1
  348. 1
  349. -1
  350. 1

Next we present the confusion matrices to validate the predictions:

In [14]:
cm.2<-table(test.set.labels,predictions.2)
cm.2
               predictions.2
test.set.labels  -1   1
             -1 155  22
             1   18 155
In [15]:
cm.5<-table(test.set.labels,predictions.5)
cm.5
               predictions.5
test.set.labels  -1   1
             -1 172   5
             1    1 172
In [16]:
cm.10<-table(test.set.labels,predictions.10)
cm.10
               predictions.10
test.set.labels  -1   1
             -1 163  14
             1    5 168
In [17]:
acc.2<-(cm.2[1][1]+cm.2[4][1])/sum(cm.2)
caption.2<-sprintf("RBF Accuracy: %.4f",acc.2)
caption.2
'RBF Accuracy: 0.8857'
In [18]:
acc.5<-(cm.5[1][1]+cm.5[4][1])/sum(cm.5)
caption.5<-sprintf("RBF Accuracy: %.4f",acc.5)
caption.5
'RBF Accuracy: 0.9829'
In [19]:
acc.10<-(cm.10[1][1]+cm.10[4][1])/sum(cm.10)
caption.10<-sprintf("RBF Accuracy: %.4f",acc.10)
caption.10
'RBF Accuracy: 0.9457'

Next we visualize the predictions with the RBF centers:

In [20]:
plot(training.set[,]$V1,training.set[,]$V2,xlim=c(-1,1),ylim=c(-1,1),col=c("blue","black","red")[training.set.labels[]+2])
points(test.set[,1],test.set[,2],col=c("blue","black","red")[predictions.2[]+2],pch=3)
points(test.set[,1],test.set[,2],col=c("blue","black","red")[test.set.labels[]+2],pch=0)
points(rbf.model.2$centers, col="black", pch=19) 
legend("topleft",legend=c("label","prediction"),pch=c(0,3))
title(caption.2)
In [21]:
plot(training.set[,]$V1,training.set[,]$V2,xlim=c(-1,1),ylim=c(-1,1),col=c("blue","black","red")[training.set.labels[]+2])
points(test.set[,1],test.set[,2],col=c("blue","black","red")[predictions.5[]+2],pch=3)
points(test.set[,1],test.set[,2],col=c("blue","black","red")[test.set.labels[]+2],pch=0)
points(rbf.model.5$centers, col="black", pch=19) 
legend("topleft",legend=c("label","prediction"),pch=c(0,3))
title(caption.5)
In [22]:
plot(training.set[,]$V1,training.set[,]$V2,xlim=c(-1,1),ylim=c(-1,1),col=c("blue","black","red")[training.set.labels[]+2])
points(test.set[,1],test.set[,2],col=c("blue","black","red")[predictions.10[]+2],pch=3)
points(test.set[,1],test.set[,2],col=c("blue","black","red")[test.set.labels[]+2],pch=0)
points(rbf.model.10$centers, col="black", pch=19) 
legend("topleft",legend=c("label","prediction"),pch=c(0,3))
title(caption.10)

Next we compare the performance of an RBF network with 2 centers, 5 centers, and 10 centers to the performance of a neural network with 1 hidden layer containing 2 hidden nodes, 5 hidden nodes, and 10 hidden nodes, respectively. We perform the same process with the neural network as we did with the RBF network:

In [23]:
library(neuralnet)
nrepeats<-5
net.2<-neuralnet(labels ~ V1+V2,training.set,hidden=2,rep=nrepeats)
summary(net.2)
                    Length Class      Mode    
call                  5    -none-     call    
response            150    -none-     numeric 
covariate           300    -none-     numeric 
model.list            2    -none-     list    
err.fct               1    -none-     function
act.fct               1    -none-     function
linear.output         1    -none-     logical 
data                  3    data.frame list    
exclude               0    -none-     NULL    
net.result            5    -none-     list    
weights               5    -none-     list    
generalized.weights   5    -none-     list    
startweights          5    -none-     list    
result.matrix        60    -none-     numeric 
In [24]:
net.5<-neuralnet(labels ~ V1+V2,training.set,hidden=5,rep=nrepeats)
summary(net.5)
                    Length Class      Mode    
call                  5    -none-     call    
response            150    -none-     numeric 
covariate           300    -none-     numeric 
model.list            2    -none-     list    
err.fct               1    -none-     function
act.fct               1    -none-     function
linear.output         1    -none-     logical 
data                  3    data.frame list    
exclude               0    -none-     NULL    
net.result            5    -none-     list    
weights               5    -none-     list    
generalized.weights   5    -none-     list    
startweights          5    -none-     list    
result.matrix       120    -none-     numeric 
In [25]:
net.10<-neuralnet(labels ~ V1+V2,training.set,hidden=10,rep=nrepeats)
summary(net.10)
                    Length Class      Mode    
call                  5    -none-     call    
response            150    -none-     numeric 
covariate           300    -none-     numeric 
model.list            2    -none-     list    
err.fct               1    -none-     function
act.fct               1    -none-     function
linear.output         1    -none-     logical 
data                  3    data.frame list    
exclude               0    -none-     NULL    
net.result            5    -none-     list    
weights               5    -none-     list    
generalized.weights   5    -none-     list    
startweights          5    -none-     list    
result.matrix       220    -none-     numeric 
In [26]:
nn.predictions.2<-predict(net.2,test.set[,-3])
nn.predictions.2<-ifelse(nn.predictions.2>0.5,1,-1)
nn.predictions.2
1-1
2 1
3-1
4-1
5-1
6-1
7-1
8-1
10-1
11-1
15-1
16 1
18-1
19-1
21-1
22-1
23-1
24-1
25-1
26-1
28 1
30 1
32-1
34 1
35-1
36-1
37 1
38 1
39-1
41-1
......
456-1
457-1
458-1
460-1
462-1
464-1
466-1
468 1
470-1
471-1
472-1
474-1
476-1
477-1
478 1
479 1
480 1
481 1
484 1
485-1
486-1
487-1
489-1
490 1
492 1
495-1
496 1
497 1
498-1
500 1
In [27]:
nn.predictions.5<-predict(net.5,test.set[,-3])
nn.predictions.5<-ifelse(nn.predictions.5>0.5,1,-1)
nn.predictions.5
1 1
2-1
3 1
4-1
5 1
6 1
7 1
8 1
10-1
11-1
15-1
16 1
18-1
19-1
21-1
22-1
23-1
24-1
25-1
26-1
28 1
30-1
32-1
34-1
35-1
36-1
37 1
38-1
39-1
41-1
......
456-1
457-1
458-1
460-1
462-1
464-1
466 1
468 1
470-1
471-1
472-1
474-1
476-1
477-1
478 1
479 1
480-1
481 1
484 1
485-1
486-1
487-1
489-1
490 1
492 1
495-1
496-1
497 1
498-1
500 1
In [28]:
nn.predictions.10<-predict(net.10,test.set[,-3])
nn.predictions.10<-ifelse(nn.predictions.10>0.5,1,-1)
nn.predictions.10
1-1
2-1
3 1
4-1
5 1
6 1
7 1
8 1
10-1
11-1
15-1
16 1
18-1
19-1
21-1
22-1
23-1
24-1
25-1
26-1
28 1
30-1
32-1
34-1
35-1
36 1
37 1
38-1
39-1
41-1
......
456-1
457-1
458-1
460-1
462-1
464-1
466 1
468 1
470-1
471-1
472-1
474-1
476-1
477-1
478 1
479 1
480-1
481 1
484-1
485-1
486-1
487-1
489-1
490 1
492 1
495-1
496-1
497-1
498-1
500 1
In [29]:
nn.cm.2<-table(test.set.labels,nn.predictions.2)
nn.cm.2
               nn.predictions.2
test.set.labels  -1   1
             -1 168   9
             1   90  83
In [30]:
nn.cm.5<-table(test.set.labels,nn.predictions.5)
nn.cm.5
               nn.predictions.5
test.set.labels  -1   1
             -1 176   1
             1   51 122
In [31]:
nn.cm.10<-table(test.set.labels,nn.predictions.10)
nn.cm.10
               nn.predictions.10
test.set.labels  -1   1
             -1 177   0
             1   40 133
In [32]:
nn.acc.2<-(nn.cm.2[1][1]+nn.cm.2[4][1])/sum(nn.cm.2)
nn.caption.2<-sprintf("One Hidden Layer (%d nodes) NN Accuracy: %.4f",2,nn.acc.2)
nn.caption.2
'One Hidden Layer (2 nodes) NN Accuracy: 0.7171'
In [33]:
nn.acc.5<-(nn.cm.5[1][1]+nn.cm.5[4][1])/sum(nn.cm.5)
nn.caption.5<-sprintf("One Hidden Layer (%d nodes) NN Accuracy: %.4f",5,nn.acc.5)
nn.caption.5
'One Hidden Layer (5 nodes) NN Accuracy: 0.8514'
In [34]:
nn.acc.10<-(nn.cm.10[1][1]+nn.cm.10[4][1])/sum(nn.cm.10)
nn.caption.10<-sprintf("One Hidden Layer (%d nodes) NN Accuracy: %.4f",10,nn.acc.10)
nn.caption.10
'One Hidden Layer (10 nodes) NN Accuracy: 0.8857'

Finally we compare the performance of the two network types:

In [35]:
rbf.accuracy.vector<-c(acc.2, acc.5, acc.10)
nn.accuracy.vector<-c(nn.acc.2, nn.acc.5, nn.acc.10)
cenhid.vector<-c(2, 5, 10)
accuracy.comp.frame<-as.data.frame(cbind(rbf.accuracy.vector, nn.accuracy.vector, cenhid.vector))
names(accuracy.comp.frame)<-c("RBF Accuracy", "NN Accuracy", "Centers/Hidden Nodes")
accuracy.comp.frame
RBF AccuracyNN AccuracyCenters/Hidden Nodes
0.88571430.7171429 2
0.98285710.8514286 5
0.94571430.885714310

It is evident from these results that the RBF network performs much better on this datset than a Neural Network with one hidden layer. Also, it took much more time to train the Neural Network than it did the RBF network. The best overall performance was obtained with an RBF network with 5 centers.